package cn.zhoutingrui.pethome.dept.service.impl;

import cn.zhoutingrui.pethome.base.service.impl.BaseServiceImpl;
import cn.zhoutingrui.pethome.dept.doamin.Department;
import cn.zhoutingrui.pethome.dept.mapper.DepartmentMapper;
import cn.zhoutingrui.pethome.dept.query.DepartmentQuery;
import cn.zhoutingrui.pethome.dept.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    //添加
    @Override
    public void add(Department department) {
        //执行添加方法
        departmentMapper.add(department);
        //调用方法获取该数据的dirpath
        getDirPath(department);
    }

    //修改
    @Override
    public void update(Department department) {
        //判断是否将自己设为了上级部门
        if (department.getId()!=department.getParent_id()){
            getDirPath(department);
        }else {
            throw new RuntimeException("不能添加自己为上级部门");
        }
        //调用方法获取该部门的dirpath
        getDirPath(department);
    }

    //添加dirpath
    public void getDirPath(Department department){
        //判断是否是顶级部门
        if (department.getParent_id()==null){
            //做dirpath的拼接
            department.setDirPath("/"+department.getId());
        }else {
            //获取该部门的上级部门
            Department parent = departmentMapper.findById(department.getParent_id());
            //做dirpath的拼接
            department.setDirPath(parent.getDirPath()+"/"+department.getId());
        }
        //执行修改方法
        departmentMapper.update(department);
    }

    //删除
    @Override
    public void delete(Long id) {
        //查询是否存在子级部门
        Long count = departmentMapper.findSon(id);
        if (count==0){
            //执行删除
            departmentMapper.delete(id);
        }else {
            throw new RuntimeException("当前部门存在子级部门,无法删除");
        }

    }


    //获取部门级树
    @Override
    public List<Department> deptTree(Long id) {
        //获取所有部门信息
        List<Department> depts = departmentMapper.findAll();
        //遍历所有部门
        for (Department dept : depts) {
            if (dept.getParent_id()!=null && dept.getId()!= id){
                //当该部门不是顶级父类时
                for (Department parent : depts) {
                    if (dept.getParent_id()==parent.getId()){
                        //相等表示找到父类,把该部门添加到父类children中
                        parent.getChildren().add(dept);
                        break;
                    }
                }
            }
        }
        //获取顶级父类
        List<Department> topParent = depts.stream()
                .filter(department -> department
                .getParent_id() == null)
                .collect(Collectors.toList());
        return topParent;
    }

}
